package com.shelpe.services.sinbad.controller.sinbad;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.shelpe.services.sinbad.constvalue.CacheBusinessCode.BusinessCode;
import com.shelpe.services.sinbad.controller.dto.AeUserPriceDto;
import com.shelpe.services.sinbad.service.cache.CacheService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @description
 * @author yechen@shelpe.com
 * @date 2018年3月15日上午10:01:02
 */
@RestController
@RequestMapping(value = "/sinbadservice/v2")
@Api("ae相关的API")
public class AeAboutController {
	@Autowired
	private CacheService cacheService;

	/**
	 * 
	 * @description ae系统修改价格区间的上下限
	 * @param userID
	 * @param dto
	 * @return
	 * @author yechen@shelpe.com
	 * @date 2018年3月15日上午10:02:34
	 */
	@ApiOperation(value = "创建用户级别关键词的上下限")
	@RequestMapping(method = RequestMethod.POST, path = "/users/{user_id}/ae/keywords/@price")
	public AeUserPriceDto createAeUserPrice(@PathVariable("user_id") long userID, @RequestBody AeUserPriceDto dto) {
		Assert.isTrue(dto != null && dto.getMixPrice() != null && dto.getMixPrice() > 0 && dto.getMaxPrice() != null
				&& dto.getMaxPrice() > 0 && dto.getMaxPrice() > dto.getMixPrice(), "请给价格上限，下限传入有意义的值");
		cacheService.putObject(BusinessCode.aeuserlevelkeywordprice, String.valueOf(userID), dto);
		return dto;
	}

	@ApiOperation(value = "获取用户级别关键词的上下限")
	@RequestMapping(method = RequestMethod.GET, path = "/users/{user_id}/ae/keywords/@price")
	public AeUserPriceDto getAeUserPrice(@PathVariable("user_id") long userID) {
		Object obj = cacheService.getObject(BusinessCode.aeuserlevelkeywordprice, String.valueOf(userID));
		if (obj != null && obj instanceof AeUserPriceDto) {
			AeUserPriceDto dto = (AeUserPriceDto) obj;
			return dto;
		} else {
			AeUserPriceDto retDto = new AeUserPriceDto();
			retDto.setMixPrice(0);
			retDto.setMaxPrice(0);
			return retDto;
		}
	}
}
